package com.ruoyi.zhmk.service.impl;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.faceApi.entity.FaceIdentification;
import com.ruoyi.zhmk.domain.ZhmkDoor;
import com.ruoyi.zhmk.mapper.ZhmkDoorMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zhmk.mapper.ZhmkUserinoutMapper;
import com.ruoyi.zhmk.domain.ZhmkUserinout;
import com.ruoyi.zhmk.service.IZhmkUserinoutService;
import com.ruoyi.common.core.text.Convert;

/**
 * 人员进出记录Service业务层处理
 * 
 * @author sxl
 * @date 2021-06-15
 */
@Service
public class ZhmkUserinoutServiceImpl implements IZhmkUserinoutService 
{
    @Autowired
    private ZhmkUserinoutMapper zhmkUserinoutMapper;
    @Autowired
    private ZhmkDoorMapper zhmkDoorMapper;

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 查询人员进出记录
     * 
     * @param uinoutId 人员进出记录ID
     * @return 人员进出记录
     */
    @Override
    public ZhmkUserinout selectZhmkUserinoutById(Long uinoutId)
    {
        return zhmkUserinoutMapper.selectZhmkUserinoutById(uinoutId);
    }

    /**
     * 查询人员进出记录列表
     * 
     * @param zhmkUserinout 人员进出记录
     * @return 人员进出记录
     */
    @Override
    public List<ZhmkUserinout> selectZhmkUserinoutList(ZhmkUserinout zhmkUserinout)
    {
        return zhmkUserinoutMapper.selectZhmkUserinoutList(zhmkUserinout);
    }

    /**
     * 新增人员进出记录
     * 
     * @param zhmkUserinout 人员进出记录
     * @return 结果
     */
    @Override
    public int insertZhmkUserinout(ZhmkUserinout zhmkUserinout)
    {
        zhmkUserinout.setCreateTime(DateUtils.getNowDate());
        return zhmkUserinoutMapper.insertZhmkUserinout(zhmkUserinout);
    }

    /**
     * 修改人员进出记录
     * 
     * @param zhmkUserinout 人员进出记录
     * @return 结果
     */
    @Override
    public int updateZhmkUserinout(ZhmkUserinout zhmkUserinout)
    {
        zhmkUserinout.setUpdateTime(DateUtils.getNowDate());
        return zhmkUserinoutMapper.updateZhmkUserinout(zhmkUserinout);
    }

    /**
     * 删除人员进出记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteZhmkUserinoutByIds(String ids)
    {
        return zhmkUserinoutMapper.deleteZhmkUserinoutByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除人员进出记录信息
     * 
     * @param uinoutId 人员进出记录ID
     * @return 结果
     */
    @Override
    public int deleteZhmkUserinoutById(Long uinoutId)
    {
        return zhmkUserinoutMapper.deleteZhmkUserinoutById(uinoutId);
    }

    /**
     * 保存用户进出进出信息
     * @param faceIdentification
     * @return
     */
    @Override
    public int addZhmkUserInfo(FaceIdentification faceIdentification) {
        //保存图片
        String filePath = RuoYiConfig.getUploadPath();
        String fileName = System.currentTimeMillis()+"";
        String dateStr = simpleDateFormat.format(new Date());
        String filePathname = filePath+"/person/"+dateStr+"/"+fileName+".jpg";
        File file = new File(filePath+"/person/"+dateStr+"/");
        if(!file.exists()){
            file.mkdirs();
        }
        String webUrl = "/profile/upload/person/"+dateStr+"/"+fileName+".jpg";
        byte[] halfBodyImage = faceIdentification.getHalfBodyImage();
        if(halfBodyImage != null){
            try {
                DataOutputStream out  = new DataOutputStream(new FileOutputStream(filePathname));
                out.write(halfBodyImage);
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        //查询设备表
        ZhmkDoor zhmkDoor = zhmkDoorMapper.selectZhmkDoorByDeviceId(faceIdentification.getDeviceID());

        ZhmkUserinout zhmkUserinout = new ZhmkUserinout();
        zhmkUserinout.setUserName(faceIdentification.getName());
        zhmkUserinout.setUserId(faceIdentification.getPersonId());
        zhmkUserinout.setCreateTime(DateUtils.getNowDate());
        zhmkUserinout.setUinoutTime(DateUtils.getTime());
        zhmkUserinout.setAvatar(webUrl);
        if(zhmkDoor != null){
            zhmkUserinout.setOutdoorSite(zhmkDoor.getDoorPosition());
        }

        zhmkUserinoutMapper.insertZhmkUserinout(zhmkUserinout);
        return 1;
    }
}
